import sys
from PyQt5.QtWidgets import QApplication,QWidget

import xx #xx是ui文件名，下同

class QmyWidget(QWidget):
    def __init__(self,parent = None):
        super().__init__(parent)
        self.ui = xx.Ui_Form()
        self.ui.setupUi(self)

# -*- encoding=utf8 -*-
__author__ = "W"

from airtest.core.api import *
from airtest.report.report import *

auto_setup(__file__, logdir=True, devices=["android://127.0.0.1:5037/emulator-5554?cap_method=MINICAP&&ori_method=MINICAPORI&&touch_method=MINITOUCH"])


def trytouch(img):  # 随便弄了一个1秒内尝试点击一个图片，没有找到图片就不点击，且不会因报错而停止的函数。
    try:
        touch(wait(img, timeout=1))
    except:
        pass


def try_panduan(buff_img):  # 判断BUFF那里用的函数
    try:
        result = wait(buff_img, timeout=1)
        return result
    except:
        return


def panduanguoguan():
    while not exists(Template(r"tpl1641989217172.png", record_pos=(-0.366, 0.15), resolution=(1440, 810))):  # 判断过关
        sleep(4.0)  # 没有成功通过就等4秒，然后继续循环找成功通过
    else:
        sleep(2.0)  # 找到了之后，为了防止在“成功通关”这四个字从画面左边飞到画面内的瞬间识别到并立刻进行点击而设置的一个等待时间。（因为计算机执行速度非常快。）
    touch((1000, 490))  # 识别到了点击一下
    sleep(1.0)
    while not exists(Template(r"tpl1641989331329.png", record_pos=(0.296, 0.07), resolution=(1440, 810))):  # 判断有没有这个图片
        swipe((670, 500), vector=[-0.3, 0])  # 判断没有，就自右往左滑动屏幕，移到右边，移完回去继续判断图片
    else:
        touch(Template(r"tpl1641989331329.png", record_pos=(0.296, 0.07), resolution=(1440, 810)))  # 判断到了就点它
    sleep(1.0)
    touch(Template(r"tpl1641989346223.png", record_pos=(0.339, 0.147), resolution=(1440, 810)))
    sleep(1.5)


def likai():
    while exists(Template(r"tpl1649067824301.png", record_pos=(-0.013, 0.105), resolution=(1440, 810))):
        touch(Template(r"tpl1649067824301.png", record_pos=(-0.013, 0.105), resolution=(1440, 810)))
        sleep(2.0)
        touch(Template(r"tpl1649067824301.png", record_pos=(-0.013, 0.105), resolution=(1440, 810)))
        break

    sleep(1.0)
    touch(Template(r"tpl1649066161675.png", record_pos=(0.423, 0.147), resolution=(1440, 810)))
    sleep(1.0)
    touch(Template(r"tpl1649066179405.png", record_pos=(0.398, 0.146), resolution=(1440, 810)))
    sleep(5.0)
    touch((45, 30))
    sleep(1.0)
    # 第二次探索如果卡主，可以适当将这里的sleep时间增加一点，比如sleep(3.0)，sleep可以理解为等待一段时间的意思
    touch(Template(r"tpl1641990570636.png", record_pos=(0.412, -0.018), resolution=(1440, 810)))
    sleep(1.0)
    # 这个对象likai()里的这些sleep时间都可以适当改一下，改到适合自己模拟器运行加载速度的等待时间
    touch(Template(r"tpl1641990587770.png", record_pos=(0.159, 0.106), resolution=(1440, 810)))
    sleep(3.0)
    touch((700, 730))  # 等一段时间点击画面进入下一个结算的界面
    sleep(3.0)
    touch((700, 730))  # 等一段时间点击画面进入下一个结算的界面，点完这次应该就会回到肉鸽主菜单
    sleep(3.0)
    trytouch(Template(r"tpl1646104849842.png", threshold=0.9000000000000001, record_pos=(0.001, -0.204),
                      resolution=(1440, 810)))
    sleep(1.0)
    while not exists(Template(r"tpl1646224294375.png", threshold=0.9000000000000001, record_pos=(0.405, 0.191),
                              resolution=(1440, 810))):
        touch((690, 40))


def kaishi():
    while not exists(Template(r"tpl1646224294375.png", threshold=0.9000000000000001, record_pos=(0.405, 0.191),
                              resolution=(1440, 810))):
        touch((690, 40))
    sleep(1.0)
    touch((690, 40), times=10)
    sleep(1.0)
    touch((1300, 650))
    sleep(1.0)
    # 可以回到肉鸽主界面但不会进行下一次探索的，请修改likai()对象里面最下面的那个sleep时间'''
    while not exists(Template(r"tpl1649064803814.png", record_pos=(0.057, 0.072), resolution=(1440, 810))):
        # 精二高级的山比较稳定过关，所以选了这个分队'''
        swipe((670, 500), vector=[-0.1861, 0.007])
    else:
        touch(Template(r"tpl1649064803814.png", record_pos=(0.057, 0.072), resolution=(1440, 810)))

        # 有需要的就把这两个突击战术分队的图片换成自己想要的分队吧，用左侧Airtest辅助窗里的功能就可以截图生成自己的代码了。'''
        touch(Template(r"tpl1641991004571.png", record_pos=(0.287, 0.182), resolution=(1440, 810)))
    sleep(1.0)
    touch(Template(r"tpl1641991034647.png", threshold=0.9000000000000001, record_pos=(0.11, 0.062),
                   resolution=(1440, 810)))
    touch(Template(r"tpl1641991004571.png", record_pos=(0.287, 0.182), resolution=(1440, 810)))
    sleep(2.0)
    touch((390, 600))  # 点击第一张招募券下面的招募按钮的坐标。
    sleep(1.0)

    if exists(Template(r"tpl1641991518242.png", record_pos=(0.051, -0.149),
                       resolution=(1440, 810))):  # 判断画面内有没有山，有就选，没有就执行else:后面的内容
        touch(Template(r"tpl1641991518242.png", record_pos=(0.051, -0.149), resolution=(1440, 810)))
        sleep(2.0)
        touch((1250, 750))
        sleep(3.0)
    else:  # 上面if是选择自己的山，else下面的部分是选择用助战
        touch((1220, 30))
        while not exists(Template(r"tpl1641991335442.png", threshold=0.9000000000000001, target_pos=6,
                                  record_pos=(-0.222, 0.222),
                                  resolution=(1440, 810))):  # 因为有时候刷新到的山在第三列，招募按钮在右边画面外，所以设置的一个判断。
            touch((1300, 30))
            while not exists(Template(r"tpl1641991147628.png", record_pos=(0.025, -0.117), resolution=(1440, 810))):
                touch((1300, 30))
            else:
                pass
        else:
            touch(Template(r"tpl1641991335442.png", threshold=0.9000000000000001, target_pos=6,
                           record_pos=(-0.222, 0.222), resolution=(1440, 810)))
        sleep(1.0)
        touch(Template(r"tpl1641991373517.png", record_pos=(0.157, 0.124), resolution=(1440, 810)))
        sleep(2.0)
    while not exists(Template(r"tpl1641991518242.png", record_pos=(0.051, -0.149),
                              resolution=(1440, 810))):  # 判断画面是否在那个招到了干员的画面。
        sleep(1.0)
    else:
        sleep(1.0)
    touch((700, 730))  # 上面判断到了在那个画面再点击一下屏幕进入下一步。
    sleep(1.0)
    for i in range(2):
        touch(Template(r"tpl1646060523902.png", threshold=0.9000000000000001, record_pos=(-0.001, 0.145),
                       resolution=(1440, 810)))
        sleep(1.0)
        touch(Template(r"tpl1646271295365.png", threshold=0.9000000000000001, record_pos=(0.277, 0.247),
                       resolution=(1440, 810)))
        sleep(1.0)
        touch(Template(r"tpl1646271313530.png", threshold=0.9000000000000001, record_pos=(0.157, 0.106),
                       resolution=(1440, 810)))
        sleep(1.0)
    touch(Template(r"tpl1641991718574.png", record_pos=(0.441, -0.006), resolution=(1440, 810)))
    sleep(5.0)


def panduanbuff():
    buff_imgs = [Template(r"tpl1646058972476.png", threshold=0.9000000000000001, record_pos=(0.047, -0.256),
                          resolution=(1440, 810)), ]
    panduan_time = 0
    while (panduan_time < 1):
        for buff_img in buff_imgs:
            if try_panduan(buff_img) == None:
                panduan_time += 1
                continue
            else:
                panduan_time = 0
                likai()
                kaishi()
                break


'''
这里本来想弄个判断走向功能，但比想象中的要难所以鸽了。
这里只实现了判断在同一行里面是否有两个不期而遇，如果有就传回这一行的Y轴坐标值。
def panduan_map():
    touchY=0 #最终传出去的结果 Y坐标的数值
    sleep(1.0)
    swipe((670,500),vector=[-0.2,0],duration=1)
    sleep(1.0)
    results = find_all(Template(r"tpl1646105756560.png", record_pos=(-0.024, -0.048), resolution=(1440, 810))) #找所有不期而遇的坐标
    result = [] #用于添加所有不期而遇坐标的Y轴数值
    for i in range(len(results)):
        result.append(results[i-1]['result'][0]);#添加所有不期而遇坐标的Y轴数值
    times=0
    for f_int in result:
        times+=1
        if times < len(result):
            for s_int in result[times:]:
                if abs(f_int-s_int) < 100:
                    touchY = f_int
                    break
                else:
                    continue
            break
        else:
            break
    return touchY
'''


def pick_member():
    if try_panduan(Template(r"tpl1641994682261.png", record_pos=(-0.32, -0.146),
                            resolution=(1440, 810))) != None:  # 判断行动选干员界面有没有山
        pass  # 有就pass不管
    else:
        touch((250, 190))  # 没有就执行这段选山和选择技能。
        sleep(1.0)
        touch((680, 160))
        sleep(1.0)
        touch((200, 600))
        sleep(1.0)
        touch((1270, 760))


def lipaoxiaodui():  # 注释参考xunshouxiaowu()里的注释
    touch(Template(r"tpl1646126259273.png", threshold=0.9000000000000001, record_pos=(0.397, 0.101),
                   resolution=(1440, 810)))
    sleep(1.0)
    pick_member()
    touch(Template(r"tpl1641988492692.png", record_pos=(0.322, 0.242), resolution=(1440, 810)))
    while not exists(Template(r"tpl1646226625337.png", threshold=0.9000000000000001, record_pos=(0.044, -0.258),
                              resolution=(1440, 810))):
        sleep(1)
    sleep(2)
    touch(Template(r"tpl1646126432694.png", threshold=0.9000000000000001, record_pos=(0.362, -0.25),
                   resolution=(1440, 810)))
    sleep(3)
    swipe(Template(r"tpl1641988605026.png", record_pos=(0.457, 0.237), resolution=(1440, 810)), (575, 386),
          duration=2)  # 拖干员到位置
    sleep(1.0)
    swipe((560, 382), (1380, 400))  # 设置朝向
    sleep(4.0)
    touch((455, 364))  # 点山
    sleep(2.0)
    touch((1000, 490))  # 开山技能
    panduanguoguan()


def yuchongweiban():  # 注释参考xunshouxiaowu()里的注释
    touch(Template(r"tpl1646126259273.png", threshold=0.9000000000000001, record_pos=(0.397, 0.101),
                   resolution=(1440, 810)))
    sleep(1.5)
    pick_member()
    touch(Template(r"tpl1641988492692.png", record_pos=(0.322, 0.242), resolution=(1440, 810)))
    while not exists(Template(r"tpl1646226625337.png", threshold=0.9000000000000001, record_pos=(0.044, -0.258),
                              resolution=(1440, 810))):
        sleep(1)
    sleep(2)
    touch(Template(r"tpl1646126432694.png", threshold=0.9, record_pos=(0.362, -0.25), resolution=(1440, 810)))
    sleep(3)
    swipe(Template(r"tpl1641988605026.png", record_pos=(0.457, 0.237), resolution=(1440, 810)), (667, 432),
          duration=2)  # 拖干员到位置
    sleep(1.0)
    swipe((667, 432), (1380, 400))  # 设置朝向
    sleep(4.0)
    touch((575, 411))  # 点山
    sleep(1.0)
    touch((930, 450))  # 开山技能
    panduanguoguan()


def yiwai():  # 注释参考xunshouxiaowu()里的注释
    touch(Template(r"tpl1646126259273.png", threshold=0.9000000000000001, record_pos=(0.397, 0.101),
                   resolution=(1440, 810)))
    sleep(1.5)
    pick_member()
    touch(Template(r"tpl1641988492692.png", record_pos=(0.322, 0.242), resolution=(1440, 810)))
    while not exists(Template(r"tpl1646226625337.png", threshold=0.9000000000000001, record_pos=(0.044, -0.258),
                              resolution=(1440, 810))):
        sleep(1)
    sleep(2)
    touch(Template(r"tpl1646126432694.png", threshold=0.9, record_pos=(0.362, -0.25), resolution=(1440, 810)))
    sleep(3)
    swipe(Template(r"tpl1641988605026.png", record_pos=(0.457, 0.237), resolution=(1440, 810)), (806, 370),
          duration=2)  # 拖干员到位置
    sleep(1.0)
    swipe((806, 370), (1380, 400))  # 设置朝向
    sleep(1.0)
    sleep(4.0)
    touch((715, 363))  # 点山
    sleep(2.0)
    touch((1000, 490))  # 开山技能
    panduanguoguan()


def xunshouxiaowu():  # 注释参考xunshouxiaowu()里的注释
    touch(Template(r"tpl1646126259273.png", threshold=0.9000000000000001, record_pos=(0.397, 0.101),
                   resolution=(1440, 810)))
    sleep(1.5)
    pick_member()
    touch(Template(r"tpl1641988492692.png", record_pos=(0.322, 0.242), resolution=(1440, 810)))
    while not exists(Template(r"tpl1646226625337.png", threshold=0.9000000000000001, record_pos=(0.044, -0.258),
                              resolution=(1440, 810))):
        sleep(1)
    sleep(2)
    touch(Template(r"tpl1646126432694.png", threshold=0.9, record_pos=(0.362, -0.25), resolution=(1440, 810)))
    sleep(3)
    swipe(Template(r"tpl1641988605026.png", record_pos=(0.457, 0.237), resolution=(1440, 810)), (1180, 400),
          duration=2)  # 拖干员到位置，duration是拖动过程的时间，可以自行设定适当的时间防止模拟器卡顿没下到干员，单位秒，默认是0.5秒的，有人反映下干员太快就加了这个。
    sleep(1.0)
    swipe((1180, 400), (1380, 400))  # 设置朝向，这里也可以加duration=来设定拖动时间，招着上面的样子写出来代码就好。
    sleep(5.0)
    touch((1100, 410))  # 点山
    sleep(2.0)  # 如果连点山和开技能之间都卡了，那这里的等待时间自行增加吧。
    touch((1000, 490))  # 开山技能
    panduanguoguan()


def shijian():
    touch(Template(r"tpl1641987478502.png", record_pos=(0.395, 0.096), resolution=(1440, 810)))
    sleep(4.0)
    touch((1190, 460))  # 这个点击的坐标可以在只有一个或两个选项的时候点击到最下面选项的位置。
    sleep(3.0)
    if exists(Template(r"tpl1642038786944.png", record_pos=(0.435, 0.071),
                       resolution=(1440, 810))):  # 这里是针对不期而遇里有三个选项的情况进行确认。
        touch(Template(r"tpl1642038786944.png", record_pos=(0.435, 0.071), resolution=(1440, 810)))
    else:
        touch((1190, 460))
    sleep(2.0)
    trytouch(Template(r"tpl1646274575137.png", threshold=0.9000000000000001, record_pos=(0.435, 0.205),
                      resolution=(1440, 810)))
    sleep(4.0)
    touch(Template(r"tpl1641987578489.png", record_pos=(0.0, 0.232), resolution=(1440, 810)))
    sleep(3.0)


def shandian():
    sleep(1.0)
    touch((1290, 600))
    sleep(1.0)
    c = 0
    if exists(Template(r"tpl1641989654147.png", threshold=0.9000000000000001, record_pos=(-0.105, -0.19),
                       resolution=(1440, 810))):
        touch(Template(r"tpl1641989721200.png", threshold=0.9000000000000001, record_pos=(-0.103, -0.19),
                       resolution=(1440, 810)))
        sleep(1.0)
        touch(Template(r"tpl1641989745261.png", record_pos=(-0.074, -0.037), resolution=(1440, 810)))
        sleep(1.0)
        while c < 5:  # 为解决投完了身上的源石锭还没到投资受限的情况而设置的判断（运气好试过几次17颗源石锭投完了还能继续投就卡住了的情况。）
            if try_panduan(Template(r"tpl1641989989616.png", threshold=0.9000000000000001, record_pos=(0.113, -0.04),
                                    resolution=(1440, 810))) == None:
                touch((1100, 560), times=40)
                c += 1
            else:
                sleep(1.0)
                c = 5
        else:
            likai()
    else:
        sleep(2.0)
        likai()
    kaishi()
    panduanbuff()


def panduanguanqia():
    sleep(1)
    if try_panduan(Template(r"tpl1642255403039.png", threshold=0.9000000000000001, record_pos=(0.247, -0.144),
                            resolution=(1440, 810))) != None:
        shandian()
    elif try_panduan(Template(r"tpl1642143362809.png", threshold=0.9000000000000001, record_pos=(0.247, -0.144),
                              resolution=(1440, 810))) != None:
        shijian()
    elif try_panduan(Template(r"tpl1642172966906.png", threshold=0.9000000000000001, record_pos=(0.247, -0.145),
                              resolution=(1440, 810))) != None:
        shijian()
    elif try_panduan(Template(r"tpl1646313204161.png", threshold=0.9000000000000001, record_pos=(0.243, -0.144),
                              resolution=(1440, 810))) != None:
        xunshouxiaowu()
    elif try_panduan(Template(r"tpl1646312090035.png", threshold=0.9000000000000001, record_pos=(0.244, -0.144),
                              resolution=(1440, 810))) != None:
        lipaoxiaodui()
    elif try_panduan(Template(r"tpl1646312230227.png", threshold=0.9000000000000001, record_pos=(0.221, -0.143),
                              resolution=(1440, 810))) != None:
        yiwai()
    elif try_panduan(Template(r"tpl1646312155480.png", threshold=0.9000000000000001, record_pos=(0.244, -0.144),
                              resolution=(1440, 810))) != None:
        yuchongweiban()


def jinjizuozhan():
    sleep(1)
    if try_panduan(Template(r"tpl1646313419023.png", threshold=0.9000000000000001, record_pos=(0.243, -0.171),
                            resolution=(1440, 810))) != None:
        xunshouxiaowu()
    elif try_panduan(Template(r"tpl1646313840911.png", threshold=0.9000000000000001, record_pos=(0.243, -0.172),
                              resolution=(1440, 810))) != None:
        lipaoxiaodui()
    elif try_panduan(Template(r"tpl1646314000508.png", threshold=0.9000000000000001, record_pos=(0.225, -0.172),
                              resolution=(1440, 810))) != None:
        yiwai()
    elif try_panduan(Template(r"tpl1646316209055.png", threshold=0.9000000000000001, record_pos=(0.243, -0.172),
                              resolution=(1440, 810))) != None:
        yuchongweiban()


if __name__ == "__main__":
    kaishi()
    sleep(2)
    panduanbuff()
    sleep(2)
    flag = 1
    while flag:
        touch((440, 370))
        touch((880, 370))
        touch((880, 280))
        touch((880, 210))
        touch((880, 440))
        touch((880, 510))
        sleep(1.0)
        if try_panduan(Template(r"tpl1646238898976.png", threshold=0.9000000000000001, record_pos=(0.163, 0.083),
                                resolution=(1440, 810))) != None:
            jinjizuozhan()
        else:
            panduanguanqia()

    '''touchY = panduan_map()
    while touchY==0:
        likai()
        kaishi()
        panduanbuff()
    '''

# 备用图片截图
# pass_msgs=[Template(r"tpl1642255403039.png", threshold=0.9000000000000001, record_pos=(0.247, -0.144), resolution=(1440, 810)),Template(r"tpl1642143362809.png", threshold=0.9000000000000001, record_pos=(0.247, -0.144), resolution=(1440, 810)),Template(r"tpl1642172966906.png", threshold=0.9000000000000001, record_pos=(0.247, -0.145), resolution=(1440, 810)),Template(r"tpl1646312155480.png", threshold=0.9000000000000001, record_pos=(0.244, -0.144), resolution=(1440, 810)),Template(r"tpl1646312090035.png", threshold=0.9000000000000001, record_pos=(0.244, -0.144), resolution=(1440, 810)),Template(r"tpl1646312230227.png", threshold=0.9000000000000001, record_pos=(0.221, -0.143), resolution=(1440, 810)),Template(r"tpl1646313204161.png", record_pos=(0.243, -0.144), resolution=(1440, 810))，Template(r"tpl1646313419023.png", threshold=0.9000000000000001, record_pos=(0.243, -0.171), resolution=(1440, 810)),Template(r"tpl1646313840911.png", threshold=0.9000000000000001, record_pos=(0.243, -0.172), resolution=(1440, 810)),Template(r"tpl1646314000508.png", threshold=0.9000000000000001, record_pos=(0.225, -0.172), resolution=(1440, 810)),Template(r"tpl1646316209055.png", threshold=0.9000000000000001, record_pos=(0.243, -0.172), resolution=(1440, 810))]

# print(type(wait(Template(r"tpl1646105756560.png", record_pos=(-0.024, -0.048), resolution=(1440, 810)))))Template(r"tpl1646064778060.png", threshold=0.9000000000000001, record_pos=(0.047, -0.257), resolution=(1440, 810))Template(r"tpl1646238898976.png", threshold=0.9000000000000001, record_pos=(0.163, 0.083), resolution=(1440, 810))

simple_report(__file__, logpath=True)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    MainWindow = QmyWidget()
    MainWindow.show()
    n = app.exec()
    sys.exit(n)